package com.smartscreen.baseinfomanage.controller;


import com.smartscreen.baseinfomanage.dto.CreateClassRequest;
import com.smartscreen.baseinfomanage.dto.StudentInfoDTO;
import com.smartscreen.baseinfomanage.dto.UpdateClassRequest;
import com.smartscreen.baseinfomanage.entity.Grade;
import com.smartscreen.baseinfomanage.entity.SchoolClass;
import com.smartscreen.baseinfomanage.service.GradeClassService;
import com.smartscreen.common.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/base")
public class GradeClassController {

    @Autowired
    private GradeClassService gradeClassService;

    @GetMapping("/grades")
    public ApiResponse<List<Grade>> getAllGrades() {
        List<Grade> grades = gradeClassService.getAllGrades();
        return ApiResponse.success(grades);
    }

    @GetMapping("/classes")
    public ApiResponse<List<SchoolClass>> getClasses(@RequestParam(required = false) String gradeId) {
        List<SchoolClass> classes = gradeClassService.getClasses(gradeId);
        return ApiResponse.success(classes);
    }

    @PostMapping("/classes")
    public ApiResponse<SchoolClass> createClass(@RequestBody CreateClassRequest request) {
        SchoolClass schoolClass = gradeClassService.createClass(request);
        return ApiResponse.created("创建成功", schoolClass);
    }

    @GetMapping("/classes/{classId}")
    public ApiResponse<SchoolClass> getClassDetail(@PathVariable String classId) {
        SchoolClass schoolClass = gradeClassService.getClassDetail(classId);
        return ApiResponse.success(schoolClass);
    }

    @PutMapping("/classes/{classId}")
    public ApiResponse<Void> updateClass(@PathVariable String classId, @RequestBody UpdateClassRequest request) {
        gradeClassService.updateClass(classId, request);
        return ApiResponse.success("更新成功", null);
    }

    @PostMapping("/classes/batch")
    public Map<String, Map<String, Object>> getClassesBatch(@RequestBody List<String> classIds) {
        return gradeClassService.getClassesBatchAsMap(classIds);
    }

    @PostMapping("/students/batch")
    public Map<String, Map<String, Object>> getStudentsBatch(@RequestBody List<String> studentIds) {
        return gradeClassService.getStudentsBatchAsMap(studentIds);
    }
    
    @GetMapping("/classes/{classId}/students/ids")
    public ApiResponse<List<String>> getStudentIdsByClassId(@PathVariable String classId) {
        List<String> studentIds = gradeClassService.getStudentIdsByClassId(classId);
        return ApiResponse.success(studentIds);
    }
    
    // 新增：根据学号批量获取学生信息
    @PostMapping("/students/by-numbers")
    public ApiResponse<List<StudentInfoDTO>> getStudentsByNumbers(@RequestBody List<String> studentNumbers) {
        List<StudentInfoDTO> students = gradeClassService.getStudentsByNumbers(studentNumbers);
        return ApiResponse.success(students);
    }
}